<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
          "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>QUnit for KEYUTIL private key reader 'keyutil.js'</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>

<!-- script type="text/javascript" src="qunit.js" /script -->
<!-- link rel="stylesheet" href="qunit.css" type="text/css" media="screen" -->
<script src="http://code.jquery.com/qunit/qunit-1.11.0.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.11.0.css" type="text/css" media="screen"/>

<script src="../ext/cj/cryptojs-312-core-fix.js"></script>
<script src="../ext/cj/x64-core.js"></script>
<script src="../ext/cj/cipher-core_min.js"></script>
<script src="../ext/cj/aes_min.js"></script>
<script src="../ext/cj/tripledes_min.js"></script>
<script src="../ext/cj/enc-base64_min.js"></script>
<script src="../ext/cj/md5_min.js"></script>
<script src="../ext/cj/sha1_min.js"></script>
<script src="../ext/cj/sha256_min.js"></script>
<script src="../ext/cj/sha224_min.js"></script>
<script src="../ext/cj/sha512_min.js"></script>
<script src="../ext/cj/sha384_min.js"></script>
<script src="../ext/cj/ripemd160_min.js"></script>
<script src="../ext/cj/hmac_min.js"></script>
<script src="../ext/cj/pbkdf2_min.js"></script>

<script src="../ext/yahoo.js"></script>
<script src="../ext/jsbn.js"></script>
<script src="../ext/jsbn2.js"></script>
<script src="../ext/prng4.js"></script>
<script src="../ext/rng.js"></script>
<script src="../ext/base64.js"></script>
<script src="../ext/rsa.js"></script>
<script src="../ext/rsa2.js"></script>
<script src="../ext/ec.js"></script>
<script src="../ext/ec-patch.js"></script>

<script src="../src/base64x-1.1.js"></script>
<script src="../src/asn1hex-1.1.js"></script>
<script src="../src/rsapem-1.1.js"></script>
<script src="../src/rsasign-1.2.js"></script>
<script src="../src/asn1-1.0.js"></script>
<script src="../src/ecdsa-modified-1.0.js"></script>
<script src="../src/ecparam-1.0.js"></script>
<script src="../src/x509-1.1.js"></script>
<script src="../src/keyutil-1.0.js"></script>
<script src="../src/asn1x509-1.0.js"></script>

<script type="text/javascript">
<!--
$(document).ready(function(){

var sk10pem = "" +
"-----BEGIN RSA PRIVATE KEY-----\r\n" +
"Proc-Type: 4,ENCRYPTED\r\n" +
"DEK-Info: AES-256-CBC,40555967F759530864FE022E257DE34E\r\n" +
"\r\n" +
"jV7uXajRw4cccDaliagcqiLOiQEUCe19l761pXRxzgQP+DH4rCi12T4puTdZyy6l\r\n" +
"eJHcSHZ/IC9N9iS3XNLgm/rRdUBahf0l1HOerHY76xIb74tB9v1S6sZe/IaOIYTO\r\n" +
"E/t63rzNmj33AzEup5xMyzuAninLL3jJGKwcwkTG5u+n8p7fuMPcML0L7beVBkvZ\r\n" +
"oXyySfMVB8h4u7yyZdhjU3O4K8QX1g1jntGJpQsh4ikfjM+3ddG/qaBrwzTBfC0C\r\n" +
"F9SUbv+Sz0nZX60PhAYEDhb7194ol7F5Y4QZuDhf6K6d1v8r5b9RqDo98INqwdsD\r\n" +
"1El24sGa3eBXi3lnpAytX7dOOBQKRh1uJyL3IuKyuFcNJj3OOGjBkR6imZwD895e\r\n" +
"qxLS+BASmyGm4DME6m+kltZ12LXwPgNU6+d+XQ4NXSA=\r\n" +
"-----END RSA PRIVATE KEY-----\r\n";

var sk10pem_passcode = "hogehoge";

var sk10b64 = "" +
"jV7uXajRw4cccDaliagcqiLOiQEUCe19l761pXRxzgQP+DH4rCi12T4puTdZyy6l" +
"eJHcSHZ/IC9N9iS3XNLgm/rRdUBahf0l1HOerHY76xIb74tB9v1S6sZe/IaOIYTO" +
"E/t63rzNmj33AzEup5xMyzuAninLL3jJGKwcwkTG5u+n8p7fuMPcML0L7beVBkvZ" +
"oXyySfMVB8h4u7yyZdhjU3O4K8QX1g1jntGJpQsh4ikfjM+3ddG/qaBrwzTBfC0C" +
"F9SUbv+Sz0nZX60PhAYEDhb7194ol7F5Y4QZuDhf6K6d1v8r5b9RqDo98INqwdsD" +
"1El24sGa3eBXi3lnpAytX7dOOBQKRh1uJyL3IuKyuFcNJj3OOGjBkR6imZwD895e" +
"qxLS+BASmyGm4DME6m+kltZ12LXwPgNU6+d+XQ4NXSA=";

var sk10dechex = "30820139020100024100b1e5aa7d6827b8c69037b269907bd8f4a7a1e6b798776e3be519fbda2966260dd56f6f3389d489a276a0a1e1b19e51328bc2d6a623f24be6b2e6718f3b292d5b02030100010240763582206ac15a4ab7320b5f921e797fb894205706fcf546df6970acfaad61c2db1fb6f9e335a1867c0f7c40a2ad39aa92b9312459bbf90d5eab831793c69321022100e584720c8f676623e94e7fb8616e667d66cd379559ffe36acd9068495e68adc5022100c66c72386fbdeded99c5963422d7380c19100757a4dcec581bd645d3d924409f0220101798337f3e7dafd8e4d319a763293673a5c1d6ebe7801775f0410f9bd9f50d02201bff2b329959a41b9549d2c2b273c97db37f9679a05267aa9aeae5d959570b23022043ea618b8f31af3aa762171cf2b5e9d563c5041a99a2217b6c692f807e18f81a";

var sk12pem = "" +
"-----BEGIN ENCRYPTED PRIVATE KEY-----\r\n" +
"MIIBpjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIU9Y9p2EfWucCAggA\r\n" +
"MBQGCCqGSIb3DQMHBAhdNa9kLcmywQSCAWAoZaR4ib7mOcfpJQr7lyp/ume0srhJ\r\n" +
"6G1GuYaZzh/At3EHVrPtH9i8OaLiS/nIitWTdd1tITzWAIGNy1jDasvLUEk+agH2\r\n" +
"FGOuGfN86bNVUVbtHxkzpslNbcZcsNZtDvrDRJoZo2nAlcNW1tICpfzMTCBl/1Pl\r\n" +
"LNqit+IdcZVYRU8DJB0+AsaXevxBp7ifpthfHq2+9kCcSCqnonAH3pnsGlcBNinP\r\n" +
"M4L2QQ6YMMSRl7nCZNT4/AJg+OyKGzNgQpcRfgrxsQXA8/4GrAcMF0YqaRS6Am6M\r\n" +
"y42+85E5qsMYmQQu+PZDDOfmaNomwT5fA4TAp0nJP0zX8gBTBLywMubPlmuMWo4N\r\n" +
"XlHKSl4Wn1sQlJOhWOAIsamdtXYPryDcQGEeiHb9Y83f9di4e9JlViZVkv+iPcGv\r\n" +
"IjsZNp6zmlqf/RXnETsJjGd0TXRWaEdu+XOOyVyPskX2177X9DUJoD31\r\n" +
"-----END ENCRYPTED PRIVATE KEY-----\r\n";

var sk12pem_passcode = "hoge";

var sk12hex = "308201a6304006092a864886f70d01050d3033301b06092a864886f70d01050c300e040853d63da7611f5ae702020800301406082a864886f70d030704085d35af642dc9b2c1048201602865a47889bee639c7e9250afb972a7fba67b4b2b849e86d46b98699ce1fc0b7710756b3ed1fd8bc39a2e24bf9c88ad59375dd6d213cd600818dcb58c36acbcb50493e6a01f61463ae19f37ce9b3555156ed1f1933a6c94d6dc65cb0d66d0efac3449a19a369c095c356d6d202a5fccc4c2065ff53e52cdaa2b7e21d719558454f03241d3e02c6977afc41a7b89fa6d85f1eadbef6409c482aa7a27007de99ec1a57013629cf3382f6410e9830c49197b9c264d4f8fc0260f8ec8a1b33604297117e0af1b105c0f3fe06ac070c17462a6914ba026e8ccb8dbef39139aac31899042ef8f6430ce7e668da26c13e5f0384c0a749c93f4cd7f2005304bcb032e6cf966b8c5a8e0d5e51ca4a5e169f5b109493a158e008b1a99db5760faf20dc40611e8876fd63cddff5d8b87bd26556265592ffa23dc1af223b19369eb39a5a9ffd15e7113b098c67744d745668476ef9738ec95c8fb245f6d7bed7f43509a03df5"; // hexadecimal of k1.2.der

var sk12ciphertext = "2865a47889bee639c7e9250afb972a7fba67b4b2b849e86d46b98699ce1fc0b7710756b3ed1fd8bc39a2e24bf9c88ad59375dd6d213cd600818dcb58c36acbcb50493e6a01f61463ae19f37ce9b3555156ed1f1933a6c94d6dc65cb0d66d0efac3449a19a369c095c356d6d202a5fccc4c2065ff53e52cdaa2b7e21d719558454f03241d3e02c6977afc41a7b89fa6d85f1eadbef6409c482aa7a27007de99ec1a57013629cf3382f6410e9830c49197b9c264d4f8fc0260f8ec8a1b33604297117e0af1b105c0f3fe06ac070c17462a6914ba026e8ccb8dbef39139aac31899042ef8f6430ce7e668da26c13e5f0384c0a749c93f4cd7f2005304bcb032e6cf966b8c5a8e0d5e51ca4a5e169f5b109493a158e008b1a99db5760faf20dc40611e8876fd63cddff5d8b87bd26556265592ffa23dc1af223b19369eb39a5a9ffd15e7113b098c67744d745668476ef9738ec95c8fb245f6d7bed7f43509a03df5"; // hexadecimal encrypted key(=ciphertext) of k1.2.der

var sk12plainhex = "30820155020100300d06092a864886f70d01010105000482013f3082013b020100024100dc6d990d4a71c769ec704db7cce04da8b8f15bfad7b6e3e129e191c30df7db0243635a8ffb293a6e4a74599911c0d15ae8e72d9c84bd8dcf19ffee839fd051670203010001024100ac15f412e8378393323f4aad5f890d97d72ab6c0528039cfa33eb2e930927bab09994944a11e1cdcf4153f1c8d9c1825bdeed0a1e86b1c268b42e1c348ad3f61022100f474097558bfd1ab88f80737e07236ca25e93c4a38f1b37bf7138c84ebf94dfb022100e6d70ba0280830e2da884114184425cb4ffd02598e5167654ac8c5cb558d0a8502202c69c4aa2854b98063e344547797e8b2738edba054963d58e17c10c4b9a6d2ef02205712264072d924176d7697958cd1a7afd8ac0686e137b35871ace96e99066a41022100ec36209834b8826b005ba599d8c3fced3d50022b81303a9532faed7d70c07424"; // hexadecimal of k1.2.dec.der

// _data/0csr1/key2.pub
var key2pub = (function() {/*
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5/2QHMBBvirriuchipR5
02Ra3oN/jduXlIFi0eWpQem2S0IrKwgiBX9+hNnjhqMhOXp4k/6IuCOH6PIRpJSk
F1N/tu7ypzTVwnT8LQof5daRQteHFPU3w3MelduE5nGPjJl8jcV36jtvt7nnkZjJ
vZPluQLNAt6azIvI31BQqNID1x3RIDTxVa6A952undto669sUuOcjUCFxwYCk3u5
m89RpayoyAEK3rq6XbSF+spirWLHeDF0d58hMGvbwI8YPnp7dNRPU08Wg9QUJE8l
JEfMyAXOYa2eKRk9mqB7jkEYDHiqNPzjsm3CT0amn8e2B+dQUYgSe3Rnj58ToiIr
xQIDAQAB
-----END PUBLIC KEY-----
*/}).toString().match(/\/\*([^]*)\*\//)[1];

// _data/0csr1/key2.prv
var key2prv = (function() {/*
-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEA5/2QHMBBvirriuchipR502Ra3oN/jduXlIFi0eWpQem2S0Ir
KwgiBX9+hNnjhqMhOXp4k/6IuCOH6PIRpJSkF1N/tu7ypzTVwnT8LQof5daRQteH
FPU3w3MelduE5nGPjJl8jcV36jtvt7nnkZjJvZPluQLNAt6azIvI31BQqNID1x3R
IDTxVa6A952undto669sUuOcjUCFxwYCk3u5m89RpayoyAEK3rq6XbSF+spirWLH
eDF0d58hMGvbwI8YPnp7dNRPU08Wg9QUJE8lJEfMyAXOYa2eKRk9mqB7jkEYDHiq
NPzjsm3CT0amn8e2B+dQUYgSe3Rnj58ToiIrxQIDAQABAoIBAQCeXYe9OLkoWiBE
yBcrhMPNPJqTf2zZp+fH5rNQ2dg7OJHKujbBv2GKI0h45EzfogiSNjqNvJoUKZ0j
teu5UAbcLZHnjSJ0Zirt/fIPxPOVcG3lFUAPXH5em/1rC1mj/i2iKzzNgtmjOkO/
RdcAmy5MjeOp8nGE3fUKYPoVB7yoILi44W7yLbSUctyFDAPCGLyjyN0N3Yq5dcyv
5F+Ec3aQhEVQ8282iAcP6/gQBg/fC1SnIOLd8UGM0EyuFhxkH5L5lQDT4obcEStz
F8lm6+53WTd9nTTNE9GRDBXySFVQmX82lfWjcLW2+8cXgzC04ozpRrQnH+stgRUC
67OcSX0ZAoGBAPwTuZwJirT3gMQgcyTozFoLGD1kRCjBW18tw0pE8C/ZAHaT4DaD
kDpzWddkiyCHSInyGIXcT1gbBgvMF4wqIYzMRjtlj88mgK//gHLhWjiCVTh76wgG
3XmLsOiL+cxjFX/XQWI5kocABzYbJThRjxOxi1zQ3MFOZVmR7LRQuvCfAoGBAOuZ
0CEQI1A2LBWJAl0KAgHcre4VfBMETTbFecs4Pul4fqSQcxlSU7x43ZLUAEEWAs3P
njN4Z6WmANdrEMlAdSv+PO8nbjA7gC5BMrlzc1TTnj2Z42SYT9AsTe+N/dAWv7Kf
AFjDVfwEKY9a21sChU0oV+ohcJsExeM5JXonulUbAoGBANlx1nmwjXNf0Tbd2xWq
r6S4z1cd5+HUMZKf8kddwmcz5L9k/lNoGAEjwgsYkx8jM8KzEM5f5h3VadjBXEqv
iTNgfvD7whrqVrX7S/1NQbtfSk4oLu1EpsYTZy6imH/jtQcLUz589VX5Az98GRZE
3IwbamFNVjB2BN1IEIzW2vt3AoGAMrpgnO3bnKa9YTmp35yLCBma/Zt0pqeTdIqa
ic4nhZ+jLQ3JKpeWXgtsMfQhxeHWYrgkaRKwxnLy1W9BIwQU9o78UcYcrJfzJoJ1
SIb6OzaBaOQcmQlOjhUsBrt8/fQX/H7N1U7h0ayXwj0e4/6r8wwPueLPWyFw9lbU
ofOx5CcCgYEAvTTcxuj6jscA33tFPa+SWM2QT8JFMxftjVTitUBdPntRWCawwzqY
yIaCKBkF8f03cTS3yz1ZiE3M1OQs1zuaUxJVTr1VpSUV+pxTuZ3gq3i/9QHadVrE
MGpevmzMV1eLDgIjvOUFkBskHqfKieaNYsP33jnqlv6Zi8Z6NVYPVDc=
-----END RSA PRIVATE KEY-----
*/}).toString().match(/\/\*([^]*)\*\//)[1];

// sample-rsasign.html
var cert1 = (function() {/*
-----BEGIN CERTIFICATE-----
MIIBvTCCASYCCQD55fNzc0WF7TANBgkqhkiG9w0BAQUFADAjMQswCQYDVQQGEwJK
UDEUMBIGA1UEChMLMDAtVEVTVC1SU0EwHhcNMTAwNTI4MDIwODUxWhcNMjAwNTI1
MDIwODUxWjAjMQswCQYDVQQGEwJKUDEUMBIGA1UEChMLMDAtVEVTVC1SU0EwgZ8w
DQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANGEYXtfgDRlWUSDn3haY4NVVQiKI9Cz
Thoua9+DxJuiseyzmBBe7Roh1RPqdvmtOHmEPbJ+kXZYhbozzPRbFGHCJyBfCLzQ
fVos9/qUQ88u83b0SFA2MGmQWQAlRtLy66EkR4rDRwTj2DzR4EEXgEKpIvo8VBs/
3+sHLF3ESgAhAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAEZ6mXFFq3AzfaqWHmCy1
ARjlauYAa8ZmUFnLm0emg9dkVBJ63aEqARhtok6bDQDzSJxiLpCEF6G4b/Nv/M/M
LyhP+OoOTmETMegAVQMq71choVJyOFE5BtQa6M/lCHEOya5QUfoRF2HF9EjRF44K
3OK+u3ivTSj3zwjtpudY5Xo=
-----END CERTIFICATE-----
*/}).toString().match(/\/\*([^]*)\*\//)[1];


// ======= TEST ===================================================================================

test("version", function() {
  equal(KEYUTIL.version, "1.0.0", "check version.");
});

// k1.[0-1].{hex|pem|der}
test("parsePKCS5PEM_k1.0key", function() {
  var info = KEYUTIL.parsePKCS5PEM(sk10pem, sk10pem_passcode);
  var dataHex = CryptoJS.enc.Hex.stringify(CryptoJS.enc.Base64.parse(info.data));
  expect(5);
  equal(info.type, "RSA", "k1.0 type is 'RSA'.");
  equal(info.cipher, "AES-256-CBC", "cipher attribute is the same");
  equal(info.ivsalt, "40555967F759530864FE022E257DE34E", "ivsalt attribute is the same.");
  equal(info.data, sk10b64, "data is the same");
  equal(dataHex, "8d5eee5da8d1c3871c7036a589a81caa22ce89011409ed7d97beb5a57471ce040ff831f8ac28b5d93e29b93759cb2ea57891dc48767f202f4df624b75cd2e09bfad175405a85fd25d4739eac763beb121bef8b41f6fd52eac65efc868e2184ce13fb7adebccd9a3df703312ea79c4ccb3b809e29cb2f78c918ac1cc244c6e6efa7f29edfb8c3dc30bd0bedb795064bd9a17cb249f31507c878bbbcb265d8635373b82bc417d60d639ed189a50b21e2291f8ccfb775d1bfa9a06bc334c17c2d0217d4946eff92cf49d95fad0f8406040e16fbd7de2897b179638419b8385fe8ae9dd6ff2be5bf51a83a3df0836ac1db03d44976e2c19adde0578b7967a40cad5fb74e38140a461d6e2722f722e2b2b8570d263dce3868c1911ea2999c03f3de5eab12d2f810129b21a6e03304ea6fa496d675d8b5f03e0354ebe77e5d0e0d5d20", "data(hex) is the same");
});

test("getKeyAndUnusedIvByPasscodeAndIvsalt", function() {
  var keyiv = KEYUTIL.getKeyAndUnusedIvByPasscodeAndIvsalt("AES-256-CBC", "hogehoge", "40555967F759530864FE022E257DE34E");
  expect(2);
  equal(keyiv.keyhex, "523c7720b6a5544d7ef212ccb5e9f78cddff8a91b8c335ce7219abbb44e6a883", "check version.");
  equal(keyiv.ivhex, "0432604dae9830352f5a02736c8684e4", "check version.");
});

test("decryptKeyB64", function() {
  var keyB64 = sk10b64;
  var alg = "AES-256-CBC";
  var keyhex = "523c7720b6a5544d7ef212ccb5e9f78cddff8a91b8c335ce7219abbb44e6a883";
  var ivhex = "40555967f759530864fe022e257de34e";
  //var ivhex = "0432604dae9830352f5a02736c8684e4";
  var decrypted = KEYUTIL.decryptKeyB64(keyB64, alg, keyhex, ivhex);
  equal(decrypted, sk10dechex, "key is the same.");
});

test("getDecryptedKeyHex_k10", function() {
  var decrypted = KEYUTIL.getDecryptedKeyHex(sk10pem, "hogehoge");
  equal(decrypted, sk10dechex, "key is the same.");
});

test("getKey RSA z1.prv.p8.pem", function() {
  var s = // =z1.prv.p8.pem
"-----BEGIN PRIVATE KEY-----\n" + 
"MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEA6GZN0rQFKRIVaPOz\n" + 
"m8l6Yue6PAm6vcTw3NjfkOt5C5u2RaK3DjESdHtNPEG1FCSJURX++I951D6uWxpO\n" + 
"NRj9WQIDAQABAkB4u8VMZGeV6aYjyw+RLH+faGFxFWDle63iHe0vfZV5+GJy+rnn\n" + 
"GrJygsJ2DEBNrmrIo6uFLPa81WL5r/gkeiZNAiEA/pvUUIZrMlVhT96XanasCx1Y\n" + 
"MN2mT1NZuUbYUSvBI58CIQDpq2gPXwXbsPQxGw5vQ2j0h0oSOJedi8YAw1xvIHB8\n" + 
"BwIhANVJy2mNwX1P4w5ahPOt6GADPB7rf2fShkZcn9gX1Fs3AiAYSbS7REk7mJ0J\n" + 
"LaLGdd9G63kLg85eldSy55uIAXsvqQIgfSYaliVtSbAgyx1Yfs3hJ+CTpNKzTNv/\n" + 
"Fx80EltYV6k=\n" + 
"-----END PRIVATE KEY-----\n";
  var pkey = KEYUTIL.getKey(s);
  equal(pkey.n.toString(16), "e8664dd2b40529121568f3b39bc97a62e7ba3c09babdc4f0dcd8df90eb790b9bb645a2b70e3112747b4d3c41b51424895115fef88f79d43eae5b1a4e3518fd59", "n of key is the same.");
  equal(pkey.e.toString(16), "10001", "e of key is the same.");
  equal(pkey.d.toString(16), "78bbc54c646795e9a623cb0f912c7f9f6861711560e57bade21ded2f7d9579f86272fab9e71ab27282c2760c404dae6ac8a3ab852cf6bcd562f9aff8247a264d", "d of key is the same.");

});

test("getEncryptedPKCS5PEMFromPrvKeyHex/1 key encryption", function() {
  var p = KEYUTIL.getEncryptedPKCS5PEMFromPrvKeyHex("RSA", sk10dechex, "hogehoge", "AES-256-CBC", "40555967F759530864FE022E257DE34E");
  equal(p, sk10pem, "encrypted");
});

test("getEncryptedPKCS5PEMFromPrvKeyHex/2 without IV", function() {
  var pem = KEYUTIL.getEncryptedPKCS5PEMFromPrvKeyHex(sk10dechex, "moge", "AES-256-CBC");
  // when AES-256-CBC, iv shall be 16 bytes (i.e. 32 chars)
  equal(pem.match(/DEK-Info: AES-256-CBC,([0-9A-F]{32})\s+/) != null, true, "iv len match: iv=" + RegExp.$1 );
});

test("getEncryptedPKCS5PEMFromPrvKeyHex/3 without Alg and IV", function() {
  var pem = KEYUTIL.getEncryptedPKCS5PEMFromPrvKeyHex(sk10dechex, "moge");
  // when AES-256-CBC, iv shall be 16 bytes (i.e. 32 chars)
  equal(pem.match(/DEK-Info: AES-256-CBC,([0-9A-F]{32})\s+/) != null, true, "iv len match: iv=" + RegExp.$1 );
});

test("getEncryptedPKCS5PEMFromPrvKeyHex/4-1 alg check", function() {
  // 1. no such alg
  throws(function() {
      KEYUTIL.getEncryptedPKCS5PEMFromPrvKeyHex("RSA", "1234af", "password", "FOOALG", "012345");
    }, 
    /^KEYUTIL unsupported algorithm:/,
    "raise exception FOOALG"
  );
});

test("getEncryptedPKCS5PEMFromPrvKeyHex/4-2 alg check", function() {
  equal(KEYUTIL.getEncryptedPKCS5PEMFromPrvKeyHex("RSA", "1234af", "password", "AES-256-CBC", "012345") != null,
        true,
        "supports alg AES-128-CBC");
});

test("getPEM, getEncryptedPKCS5PEMFromPrvKeyHex/1 key encryption", function() {
  var k = new RSAKey();
  k.readPKCS5PrvKeyHex(sk10dechex);
  var p = KEYUTIL.getPEM(k, "PKCS5PRV", "hogehoge", "AES-256-CBC", null, "40555967F759530864FE022E257DE34E");
  equal(p, sk10pem, "encrypted");
});

test("getPEM, getEncryptedPKCS5PEMFromPrvKeyHex/2 without IV", function() {
  var k = new RSAKey();
  k.readPKCS5PrvKeyHex(sk10dechex);
  var pem = KEYUTIL.getPEM(k, "PKCS5PRV", "moge", "AES-256-CBC");
  // when AES-256-CBC, iv shall be 16 bytes (i.e. 32 chars)
  equal(pem.match(/DEK-Info: AES-256-CBC,([0-9A-F]{32})\s+/) != null, true, "iv len match: iv=" + RegExp.$1 );
});

test("getPEM, getEncryptedPKCS5PEMFromPrvKeyHex/3 without Alg and IV", function() {
  var k = new RSAKey();
  k.readPKCS5PrvKeyHex(sk10dechex);
  var pem = KEYUTIL.getPEM(k, "PKCS5PRV", "moge");
  // when AES-256-CBC, iv shall be 16 bytes (i.e. 32 chars)
  equal(pem.match(/DEK-Info: DES-EDE3-CBC,([0-9A-F]{16})\s+/) != null, true, "iv len match: iv=" + RegExp.$1 + "--" + pem );
});

test("getPEM, getEncryptedPKCS5PEMFromPrvKeyHex/4 with key generation", function() {
  var k = new RSAKey();
  k.generate(512, '10001');
  var pem = KEYUTIL.getPEM(k, "PKCS5PRV", "moge");
  // when AES-256-CBC, iv shall be 16 bytes (i.e. 32 chars)
  equal(pem.match(/DEK-Info: DES-EDE3-CBC,([0-9A-F]{16})\s+/) != null, true, "iv len match: iv=" + RegExp.$1 + "--" + pem);
});

test("parseHexOfEncryptedPKCS8", function() {
  var info = KEYUTIL.parseHexOfEncryptedPKCS8(sk12hex);

  expect(5);
  equal(info.ciphertext, sk12ciphertext, "ciphertext");
  equal(info.encryptionSchemeAlg, "TripleDES", "encryptionSchemeAlg");
  equal(info.encryptionSchemeIV, "5d35af642dc9b2c1", "encryptionSchemeIV");
  equal(info.pbkdf2Salt, "53d63da7611f5ae7", "pbkdf2Salt");
  equal(info.pbkdf2Iter, 2048, "pbkdf2Iter");
});

test("getPBKDF2KeyHexFromParam", function() {
  var info = KEYUTIL.parseHexOfEncryptedPKCS8(sk12hex);
  var hKey = KEYUTIL.getPBKDF2KeyHexFromParam(info, "hoge");
  equal(hKey, "c540389de62486834ff3a208a9ab457291df38f4372ad920", "getPBKDF2KeyHexFromParam");
});

test("_getPlainPKCS8HexFromEncryptedPKCS8PEM", function() {
  var h = KEYUTIL._getPlainPKCS8HexFromEncryptedPKCS8PEM(sk12pem, "hoge");
  equal(h, sk12plainhex, "sk12pem -> sk12plainhex");
});

test("getKey(PKCS8PEM, pass)", function() {
  //var k = KEYUTIL.getRSAKeyFromEncryptedPKCS8PEM(sk12pem, "hoge");
  var k = KEYUTIL.getKey(sk12pem, "hoge");
  // n,e,d,p,q,dmp1,dmq1,coeff
  equal(k.n.toString(16), "dc6d990d4a71c769ec704db7cce04da8b8f15bfad7b6e3e129e191c30df7db0243635a8ffb293a6e4a74599911c0d15ae8e72d9c84bd8dcf19ffee839fd05167", "rsaKey.n");
  equal(k.e.toString(16), "10001", "rsaKey.e");
  equal(k.d.toString(16), "ac15f412e8378393323f4aad5f890d97d72ab6c0528039cfa33eb2e930927bab09994944a11e1cdcf4153f1c8d9c1825bdeed0a1e86b1c268b42e1c348ad3f61", "rsaKey.d");
});

test("getKey(cert1) sample-rsasign.html", function() {
  var k = KEYUTIL.getKey(cert1);
  equal(k.e.toString(16), "10001", "rsaKey.e");
});

});
-->
</script>
  
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture">test markup</div>
<a href="../">TOP</a> | 
<a href="index.html">TEST INDEX</a> | 
<a href="qunit-do-keyutil.html">keyutil</a> | 
<a href="qunit-do-keyutil-rsa.html">keyutil-rsa</a> | 
<a href="qunit-do-keyutil-dsa.html">keyutil-dsa</a> | 
<a href="qunit-do-keyutil-ec.html">keyutil-ec</a> | 
<a href="qunit-do-keyutil-eprv.html">keyutil-eprv</a> | 
<a href="qunit-do-keyutil-pub.html">keyutil-pub</a> | 
</body>
</html>


